package com.zf.nospring.rabbitmq.dead_letter;

import com.rabbitmq.client.*;
import com.zf.nospring.rabbitmq.utils.RabbitUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费死信队列，重点看下 routingKey 的变化
 */
public class DeadLetterConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitUtils.getDefaultConnection();
        Channel channel = connection.createChannel();

        // 消费的逻辑
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                long deliveryTag = envelope.getDeliveryTag();
                Channel ch = getChannel();
                // 消费死信
                String msg = new String(body);
                System.out.printf("死信消息：[%s] Envelope: %s，properties：%s\n", msg, envelope, properties);
                // 手动确认消费
                ch.basicAck(deliveryTag, false);
            }
        };

        // 消费死信队列中的消息，使用手动确认
        channel.basicConsume("test_dead_letter_queue", false, consumer);
    }
}
